package name.bingo.bing.concurrent.red.packet.front;

import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import name.bingo.bing.concurrent.red.packet.facade.TRedPacketReadFacade;
import name.bingo.bing.concurrent.red.packet.model.TRedPacket;
import name.bingo.bing.concurrent.red.packet.service.TRedPacketReadService;
import name.bingo.bing.concurrent.red.packet.service.TRedPacketWriteService;
import name.bingo.common.model.Paging;
import name.bingo.common.utils.RespHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author [ your email ]
 * @date 2019-03-22 11:09:52
 * Created by CodeGen .
 */
@Api(tags = {""})
@Slf4j
@RestController
@RequestMapping("red/packet")
public class TRedPackets {

    @Autowired
    private TRedPacketWriteService tRedPacketWriteService;

    @Autowired
    private TRedPacketReadService tRedPacketReadService;

    @Autowired
    private TRedPacketReadFacade tRedPacketReadFacade;

    @ApiOperation("查询")
    @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public TRedPacket findTRedPacket(@PathVariable Long id) {
        return RespHelper.or500(tRedPacketReadService.findById(id));
    }

    @ApiOperation("分页")
    @GetMapping(value = "/paging", produces = MediaType.APPLICATION_JSON_VALUE)
    public Paging<TRedPacket> pagingTRedPacket(@RequestParam(value = "pageNo", required = false) Integer pageNo,
                                               @RequestParam(value = "pageSize", required = false) Integer pageSize) {

        Map<String, Object> criteria = Maps.newHashMap();
        return RespHelper.or500(tRedPacketReadService.paging(pageNo, pageSize, criteria));
    }

    @ApiOperation("创建")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public Long createTRedPacket(@RequestBody TRedPacket tRedPacket) {
        return RespHelper.or500(tRedPacketWriteService.create(tRedPacket));
    }

    @ApiOperation("更新")
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public Boolean updateTRedPacket(@RequestBody TRedPacket tRedPacket) {
        return RespHelper.or500(tRedPacketWriteService.update(tRedPacket));
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Boolean deleteTRedPacket(@PathVariable Long id) {
        return false;
    }

    @ApiOperation("列表")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<TRedPacket> listTRedPacket() {
        Map<String, Object> criteria = Maps.newHashMap();
        return RespHelper.or500(tRedPacketReadService.list(criteria));
    }

    @ApiOperation("抢红包")
    @GetMapping(value = "/grap", produces = MediaType.APPLICATION_JSON_VALUE)
    public Boolean rapRedPacket(@RequestParam(value = "redPacketId", required = false) Long redPacketId,
                                @RequestParam(value = "customerId", required = false) Long customerId) {
        Map<String, Object> criteria = Maps.newHashMap();
        try {
            return tRedPacketReadFacade.grapRedPacket(redPacketId,customerId);
        }catch (Exception e){
            return false;
        }

    }
}
